package com.study.swing;

import javax.swing.*;
import java.awt.*;

public class SwingLayout {

    /**
     * 设置布局
     * 1、边框布局（BorderLayout）
     * 2、流式布局（FlowLayout）
     * 3、网格布局（gridLayout）
     * 4、盒子布局（BoxLayout）
     * 5、空布局（null）
     */
    private static void borderLayout() {

        JFrame frame = new JFrame();
        frame.setTitle("边框布局");
//        frame.setSize(400, 200);
        frame.setLayout(new BorderLayout());

        JButton b1 = new JButton("上");
//        JButton b2 = new JButton("中");
//        JButton b3 = new JButton("下");
        JButton b4 = new JButton("左");
        JButton b5 = new JButton("右");

        frame.add(b1, BorderLayout.NORTH);
//        frame.add(b2,BorderLayout.CENTER);
//        frame.add(b3,BorderLayout.SOUTH);
        frame.add(b4, BorderLayout.WEST);
        frame.add(b5, BorderLayout.EAST);
        frame.setBounds(300, 200, 600, 300);

        frame.setVisible(Boolean.TRUE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void flowLayout() {

        JFrame frame = new JFrame("流式布局");
        frame.setBounds(500, 20, 400, 200);
        frame.setVisible(Boolean.TRUE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();

        JButton but1 = new JButton("1");
        JButton but2 = new JButton("2");
        JButton but3 = new JButton("3");
        JButton but4 = new JButton("4");
        JButton but5 = new JButton("5");
        JButton but6 = new JButton("6");
        JButton but7 = new JButton("7");
        JButton but8 = new JButton("8");

        panel.add(but1);
        panel.add(but2);
        panel.add(but3);
        panel.add(but4);
        panel.add(but5);
        panel.add(but6);
        panel.add(but7);
        panel.add(but8);

        //向面板中添加FlowLayout（流式布局）管理器，将组件间的横向和纵向间隙都设置为20像素
        panel.setLayout(new FlowLayout(FlowLayout.LEADING, 20, 20));
        panel.setBackground(Color.CYAN);
        frame.add(panel);
    }

    public static void cardLayout(){

        JFrame frame = new JFrame();
        frame.setTitle("卡牌布局");
        frame.setSize(400,200);

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel panel = new JPanel(new CardLayout());

        p1.add(new JButton("登录按钮"));
        p1.add(new JButton("注册按钮"));
        p1.add(new JButton("找回密码按钮"));

        p2.add(new JTextField("请输入用户名",20));
        p2.add(new JTextField("请输入新密码",20));
        p2.add(new JTextField("请输入验证码",20));

        panel.add(p1,"card1");
        panel.add(p2,"card2");

        CardLayout cardLayout = (CardLayout)(panel.getLayout());

        cardLayout.show(panel,"card1");

        frame.add(panel);
        frame.setBounds(300,200,400,200);
        frame.setVisible(Boolean.TRUE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void gridLayout(){

        JFrame frame = new JFrame();
        frame.setTitle("网格布局");
        frame.setSize(400,200);

        // 四行四列，间隙10
        JPanel panel = new JPanel(new GridLayout(5,4,10,10));
        panel.add(new Button("("));
        panel.add(new Button(")"));
        panel.add(new Button("%"));
        panel.add(new Button("AC"));
        panel.add(new Button("7"));
        panel.add(new Button("8"));
        panel.add(new Button("9"));
        panel.add(new Button("/"));
        panel.add(new Button("4"));
        panel.add(new Button("5"));
        panel.add(new Button("6"));
        panel.add(new Button("x"));
        panel.add(new Button("1"));
        panel.add(new Button("2"));
        panel.add(new Button("3"));
        panel.add(new Button("-"));
        panel.add(new Button("0"));
        panel.add(new Button("."));
        panel.add(new Button("="));
        panel.add(new Button("+"));

        frame.add(panel);

        frame.setVisible(Boolean.TRUE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
//        borderLayout();
//        flowLayout();
        cardLayout();
//        gridLayout();
    }

}
